package com.words.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.words.entity.User;
import com.words.service.UserService;
import com.words.untils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.security.SignatureException;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j  //日志
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    JwtTokenUtils jwtTokenUtils;

    @GetMapping("/login")
    public Map<String, Object> login(User user) {
        log.info("账号：[{}]", user.getUsername());
        log.info("密码：[{}]", user.getPassword());
        System.out.println("进入登录");
        Map<String, Object> map = new HashMap<>();

        if (userService.login(user)) {

            Map<String, String> payload = new HashMap<>();
            payload.put("username", user.getUsername());

            String token = jwtTokenUtils.getToken(payload);
            map.put("state", true);
            map.put("msg", "认证成功");
            map.put("token", token);
            log.info("账号：[{}]登陆成功", user.getUsername());

        } else {
            map.put("state", false);
            map.put("msg", "认证失败");
            log.info("账号：[{}]登录失败", user.getUsername());
        }
        return map;
    }

    @GetMapping("/register")
    public Map<String,String> register(User user){
        log.info("账号：[{}]", user.getUsername());
        log.info("密码：[{}]", user.getPassword());
        System.out.println("正在注册");

        Map<String, String> map = new HashMap<>();
        if(userService.register(user)){
            map.put("msg","注册成功");
        }else {
            map.put("msg","注册失败：该账号已存在");
        }
        return map;
    }

    @GetMapping("/delete")
    public Map<String,String> delete(HttpServletRequest request){
        String token=request.getHeader("token");
        DecodedJWT verity = jwtTokenUtils.verity(token);
        String username=verity.getClaim("username").asString();

        log.info("账号：[{}] 正在删除", username);
        Map<String, String> map = new HashMap<>();
        if(userService.delete(username)){
            map.put("msg","删除成功");
        }else{
            map.put("msg","删除失败:该账号不存在");
        }

        return map;
    }

    @GetMapping("/test")
    public Map<String, Object> test(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        //
        String token=request.getHeader("token");
        DecodedJWT verity = jwtTokenUtils.verity(token);
        log.info("解码后的账号是：[{}]",verity.getClaim("username").asString());
        map.put("state", true);
        map.put("msg", "请求成功");
        return map;
    }

}
